﻿#include <GL/glut.h>
#include <iostream>
#include <math.h>
#include <corecrt_math_defines.h>

void Init();
void LineSegment();
void DrawLine(GLfloat from_x, GLfloat from_y, GLfloat to_x, GLfloat to_y);
void DrawSphere(GLfloat pos_x, GLfloat pos_y, GLfloat radius, GLfloat splitAngle);

void DrawLine(GLfloat from_x, GLfloat from_y, GLfloat to_x, GLfloat to_y)
{
	glBegin(GL_LINES);
	glVertex2f(from_x, from_y);
	glVertex2f(to_x, to_y);
	glEnd();
}

void DrawSphere(GLfloat pos_x, GLfloat pos_y, GLfloat radius, GLfloat splitAngle)
{
	GLfloat start_x = pos_x - radius;
	GLfloat start_y = pos_y;
	GLfloat temp_hudu = M_PI / 180.0f * splitAngle;
	GLfloat angle = 0;
	while (angle < 360)
	{
		angle += splitAngle;
		GLfloat temp_x = (start_x - pos_x) * cos(temp_hudu) - (start_y - pos_y) * sin(temp_hudu) + pos_x;
		GLfloat temp_y = (start_x - pos_x) * sin(temp_hudu) + (start_y - pos_y) * cos(temp_hudu) + pos_y;
		DrawLine(start_x, start_y, temp_x, temp_y);
		start_x = temp_x;
		start_y = temp_y;
	}
}

void Init()
{
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0.0, 800.0f, 0.0, 600.0f);
}

void LineSegment()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0, 0.0, 0.0);
	DrawSphere(400,300, 100, 10);
	glFlush();
}

void main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(50, 100);
	glutInitWindowSize(800, 600);
	glutCreateWindow("MuYuRender");

	Init();
	glutDisplayFunc(LineSegment);
	glutMainLoop();
}